
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
import cgi
import root.catagories.DBQuery as DBQuery
from root.catagories.htmlbuilder import htmlbuilder
from root import application as app
import os



'''
    generic day object to get the events for a particular day
    from the DB
    need to pass day as a parameter somehow
     - possibly by POSTing some information when a user clicks on the
     image of a particular day. eg: user clicks "monday", monday is POSTED,
     we use that to render the events for monday.
'''

'''
        figure out how to get POST data in Python
        use POST data to fill these in
    '''
    
'''Event Objects will be passed in the form of Arrays of event objects
     [Event 1, Event 2, Event 3] where 
     Event 1 etc = [Event Name, EventLink, Location, Event Pic URL, AgeLimit, VIP, Start_time, Date, Promoted]
'''
    
          


class EventsPage(webapp.RequestHandler):
    
   
    def get(self):
        
       
        post_data = "Events"        
        eventlist = DBQuery.DBQuery()
        search = ['Events','']
        events = eventlist.query(search)
        
        if events == "None" :
            path = os.path.join(os.path.dirname(__file__), 'NoResultsFound.html')
            self.response.out.write(template.render(path,''))
            self.response.out.write("""No Results Found. <p> <input type = "image" src = "../images/back_button.png" class = "button_top_left" value = "Back" onClick = "history.go(-1);return true;"> </p>""")
        else:
            builder = htmlbuilder()
            htmlpage = builder.generate(events, post_data)
            self.response.out.write(htmlpage)
            
            
    def post(self):
        
       
        post_data = "Events"        
        eventlist = DBQuery.DBQuery()
        search = ['Events','']
        events = eventlist.query(search)
        
        if events == "None" :
            path = os.path.join(os.path.dirname(__file__), 'NoResultsFound.html')
            self.response.out.write(template.render(path,''))
            self.response.out.write("""No Results Found. <p> <input type = "image" src = "../images/back_button.png" class = "button_top_left" value = "Back" onClick = "history.go(-1);return true;"> </p>""")
        else:
            builder = htmlbuilder()
            htmlpage = builder.generate(events, post_data)
            self.response.out.write(htmlpage)
       
            

application = webapp.WSGIApplication([('/events', EventsPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
